import ftplib
import getpass
import os



def fileTransfer():
	servername = "192.168.1.100"
	temp_server = raw_input("Default server name/address is" + servername + ". Enter your server address/name here \n or press enter to continue with default....:")
	if(len(temp_server) > 0):
		servername = temp_server
	'''myserver.com'''
	print "(default user name and password is anonymous, anonymous)"
	login = raw_input("Enter your user-name: ")
	if(len(login) == 0):
		login = 'anonymous'
	password = getpass.getpass() #raw_input("Enter your password: ")
	if(len(password) == 0):
		password = 'anonymous'
	sftp = ftplib.FTP(servername, login , password) # Connect	
	#strDir = tkFileDialog.askdirectory(parent=servername,title='')
	#sftp.cwd(raw_input("Enter directory: "))
	print "Default filetransfer directory is: public "
	strDir = raw_input("Enter directory: ")
	if(len(strDir) == 0):
		strDir = 'public'
	sftp.cwd(strDir)
	fp = fileChoose('rb') #open('todo.txt','rb') # file to send
	try:
		name = fp.name
	except AttributeError , detail: 
		print "Err: No file found"
		return		
        head, tail = os.path.split(fp.name)
	#tail = str(fp)
	#print tail	
	print sftp.storbinary('STOR '+tail, fp) # Send the file
	fp.close() # Close file and FTP
	sftp.quit()

def fileChoose(readMode):
    import Tkinter
    import tkFileDialog
    t = Tkinter.Tk()
    #t.focus()
    t.withdraw()  #hides the tk root window
    #parent=root,mode='rb',title='Choose a file'
    
    testFile = tkFileDialog.askopenfile(parent=None,mode=readMode,title='Choose a file to transfer to ftp')
    #print checksum
    #filename = "J:/smartgit-win32-setup-jre-2_1_7.zip"
    #testFile = open(filename, "rb")
    #print testFile
    return testFile

fileTransfer()


